﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using SystemSettings;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.Model.Setting;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcTestConfig
    {
        private NotifyManager _notify;
        private TestConfig _cfg;
        private TestConfig _cfgChanged;
        public string Title { get; set; } = LanguageManager.GetLanguageValue("GMPSetting");

        public string PermissionCode { get; set; } = "Per_TestConfig";

        public UcTestConfig()
        {
            InitializeComponent();
        }

        private void UcGmp_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);

            _cfg = Common.TestConfig;
            if (_cfg.Unit == "kPa")
            {
                RbkPa.IsChecked = true;
            }

            if (_cfg.Unit == "mbar")
            {
                RbMbar.IsChecked = true;
            }

            TxtFlowRange.Text = _cfg.FlowRange.ToString();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);

            string rangeStr = TxtFlowRange.Text;
            var r = double.TryParse(rangeStr, out double rangeD);
            if (!r || (rangeD < 0 || rangeD > 655.35))
            {
                _notify.ShowError(LanguageManager.GetLanguageValue("InputOutOfRange"), 3);
                return;
            }

            try
            {
                _cfgChanged = new TestConfig();
                if (RbkPa.IsChecked.HasValue && RbkPa.IsChecked.Value)
                {
                    _cfgChanged.Unit = "kPa";
                }

                if (RbMbar.IsChecked.HasValue && RbMbar.IsChecked.Value)
                {
                    _cfgChanged.Unit = "mbar";
                }

                _cfgChanged.FlowRange = rangeD;
                ConfigAndSettings.SetTestConfig(_cfgChanged);

                Common.SendFlowRange();

                var success = LanguageManager.GetLanguageValue("ActionSuccess");
                _notify.ShowSuccess(success);
                LogRepository.AddLog(LanguageManager.GetLanguageValue("OP-SetTestConfig"), "Manage");
                _triggerButtonsStatus(true);
            }
            catch (Exception ex)
            {
                _notify.ShowError(ex.Message);
            }

            _triggerButtonsStatus(true);
        }

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }

        private void RbkPa_OnChecked(object sender, RoutedEventArgs e)
        {
        }

        private void RbMbar_OnChecked(object sender, RoutedEventArgs e)
        {

        }
    }
}
